package com.gxljc.bear.crawler.util;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * Created by Administrator -> haitao on 2016/9/13.
 */
public class JsoupUtil {

	public static List<String> extractList(String html, String css) {
		ArrayList<String> lists = new ArrayList<>();
		Document doc = Jsoup.parse(html);
		for (Element e : doc.select(css)) {
			lists.add(e.text());
		}
		System.out.println("size=" + lists.size());
		return lists;
	}

	public static List<String> extractListByUrl(String url, String css) {
		String html = fetch(url);
		return extractList(html, css);
	}

	private static String fetch(String url) {
		String html = "";
		try {
			// html = new String(HtmlUtil.fetch(url));
			html = new HtmlUtil().getHtmlContent(url, "utf-8");
		} catch (Exception e) {
			System.out.println("***** fetch error for url ******");
			e.printStackTrace();
		}
		return html == null ? "" : html;
	}

	/**
	 * 
	 * @param html
	 * @param css
	 * @param attributeKey
	 * @return 获取列表 //ul[@class='news_list']/li/a | //div[@class='area']/ul/li
	 */
	public static List<String> extractAttrList(String html, String css, String attributeKey) {
		ArrayList<String> lists = new ArrayList<>();
		Document doc = Jsoup.parse(html);
		for (Element e : doc.select(css)) {
			lists.add(e.attr(attributeKey));
		}

		System.out.println("size=" + lists.size());
		return lists;
	}

	public static List<String> extractAttrListByUrl(String url, String css, String attributeKey) {
		String html = fetch(url);
		return extractAttrList(html, css, attributeKey);
	}

}
